package cn.com.computer.my.game.service;

import cn.com.computer.common.base.bean.CandyResult;
import cn.com.computer.common.enums.StatusEnum;
import cn.com.computer.common.utils.helper.ParamUtils;
import cn.com.computer.my.game.dal.domain.GameDO;
import cn.com.computer.my.game.dal.mapper.GameMapper;
import cn.com.computer.my.game.service.bo.emums.GamePlatformEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by WANGXin on 2017/5/19.
 */
public class GameService {

    private static final Logger LOGGER = LoggerFactory.getLogger(GameService.class);

    private static final Integer ZERO = 0;
    private static final String CREATE_NAME = "WANGXin";

    @Autowired
    private GameMapper gameMapper;

    /**
     * 保存
     * @param gamePlatform
     * @param gameDO
     * @return
     */
    public CandyResult save(String gamePlatform,GameDO gameDO){
        CandyResult candyResult = new CandyResult();

        // 1 check data
        if (StringUtils.isEmpty(gamePlatform)){
            candyResult.setMessage("游戏平台不能为空");
            return candyResult;
        }
        if (GamePlatformEnum.getName(gamePlatform) == null){
            candyResult.setMessage("游戏平台没有对应的数据库");
            return candyResult;
        }
        if (gameDO == null){
            candyResult.setMessage("游戏信息不能为空");
            return candyResult;
        }
        if (StringUtils.isEmpty(gameDO.getGameName())){
            candyResult.setMessage("游戏名称不能为空");
            return candyResult;
        }
        if (StringUtils.isEmpty(gameDO.getGameLanguage())){
            candyResult.setMessage("游戏语言不能为空");
            return candyResult;
        }
        if (StringUtils.isEmpty(gameDO.getGameCapacity())){
            candyResult.setMessage("游戏大小不能为空");
            return candyResult;
        }

        // 2 data preProcess
        GameDO checkGameDO = new GameDO();
        checkGameDO.setGameName(gameDO.getGameName());
        List<GameDO> gameDOListInDataBase;
        try {
            gameDOListInDataBase = gameMapper.queryGameByCondition(gamePlatform,checkGameDO);
        }catch (Exception e){
            LOGGER.error("查询所有音乐信息异常，异常数据为:["  + ParamUtils.serializable(gamePlatform,checkGameDO) + "]",e);
            candyResult.setMessage("查询所有音乐信息失败");
            return candyResult;
        }
        if (!CollectionUtils.isEmpty(gameDOListInDataBase)){
            candyResult.setMessage("数据库已经有了该音乐，保存音乐失败");
            return candyResult;
        }

        gameDO.setStatus(StatusEnum.YES.getCode());
        gameDO.setCreateBy(CREATE_NAME);
        // 3 data algorithm

        Integer saveGameCount;
        try {
            saveGameCount = gameMapper.saveGame(gamePlatform,gameDO);
        }catch (Exception e){
            LOGGER.error("保存游戏信息异常，异常数据为:["  + ParamUtils.serializable(gamePlatform,gameDO) + "]",e);
            candyResult.setMessage("保存游戏信息失败");
            return candyResult;
        }
        if (saveGameCount.equals(ZERO)){
            LOGGER.error("保存游戏信息异常，异常数据为:["  + ParamUtils.serializable(gamePlatform,gameDO) + "]");
            candyResult.setMessage("保存游戏信息失败");
            return candyResult;
        }


        candyResult.setSuccess(true);
        return candyResult;
    }


}
